//
//  File.swift
//  
//
//  Created by li on 2021/6/22.
//


/// 图片缓存助手
import SwiftUI

private protocol DogImageCache {
    subscript(_ urlString: String) -> UIImage? { get set }
}

struct DogTemporaryImageCache: DogImageCache {
    
    subscript(urlString: String) -> UIImage? {
        get {
            
            cache.object(forKey: urlString as NSString)
        }
        set {
            
            newValue == nil ? cache.removeObject(forKey: urlString as NSString) : cache.setObject(newValue!, forKey: urlString as NSString)
        }
    }
    
    private var cache = NSCache<NSString, UIImage>()
    
    /// 单利
    static var shared = DogTemporaryImageCache()
}

